<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use App\Models\Admin;
use App\Models\CompanyAdmin;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;

class AdminController extends Controller
{

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $pageSize = $request->pageSize ?: 10;
        $page = $request->page ?: 1;
        $page = $page * $pageSize - $pageSize;

        $sql = CompanyAdmin::with('admin')->with('role')->where('company_id',$request->company_id);
        $total = $sql->count();
        $data = $sql->skip($page)->take($pageSize)->orderBy('created_at','desc')->get();
        foreach ($data as $k => $v) {
            $data[$k]->name = $v->admin->name;
            $data[$k]->mobile = $v->admin->mobile;
            $data[$k]->role_name = '/';
            if($v->role){
                $data[$k]->role_name = $v->role->name;
            }

        }
        return response()->json(['code' => 200, 'message' => '操作成功','page'=>$page,'pageSize'=>$pageSize,'total'=>$total,'data'=>$data]);
    }


    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $Admin = Admin::where('mobile',$request->mobile)->first();
        if($Admin){
            //判断是否绑定了这个公司
            $CompanyAdmin = CompanyAdmin::where('admin_id',$Admin->id)->where('company_id',$request->company_id)->first();
            if($CompanyAdmin){
                return response()->json(['code' => 500, 'message' => '手机号码已存在','data'=>null]);
            }

        }else{
            $Admin = new Admin();
            $Admin->name = $request->name;
            $Admin->mobile = $request->mobile;
        }

        $CompanyAdmin = new CompanyAdmin();
        $Admin->password = Hash::make('123456');

        $CompanyAdmin->company_id = $request->company_id;

        $CompanyAdmin->role_id = $request->role_id;
        $CompanyAdmin->sex = $request->sex ?? 1;
        $CompanyAdmin->id_card = $request->id_card ?? '';
        $CompanyAdmin->idcard_file = $request->idcard_file ?? '';
        $CompanyAdmin->avatar = $request->avatar ?? '';
        $CompanyAdmin->entry_date = $request->entry_date ?? null;
        if($request->entry_date){
            $CompanyAdmin->entry_date = $request->entry_date;
        }
        $CompanyAdmin->email = $request->email ?? '';
        $CompanyAdmin->edu = $request->edu ?? '';
        $CompanyAdmin->status = $request->status;

        try {
            DB::transaction(function () use ($CompanyAdmin,$Admin) {
                $Admin->save();
                $CompanyAdmin->admin_id = $Admin->id;
                $CompanyAdmin->save();
            });
            return response()->json(['code' => 200, 'message' => '操作成功','data'=>$CompanyAdmin]);
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            return response()->json(['code' => 500, 'message' => '网络异常','data'=>null]);
        }
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request)
    {
        $Admin = Admin::find($request->admin_id);
        if(empty($Admin)){
            return response()->json(['code' => 403, 'message' => '人员不存在','data'=>null]);
        }
        if($request->mobile != $Admin->mobile){
            $ismobile = Admin::where('mobile',$request->mobile)->first();
            if($ismobile){
                return response()->json(['code' => 403, 'message' => '手机号码已存在','data'=>null]);
            }
        }

        $CompanyAdmin = CompanyAdmin::find($request->id);
        if(!$CompanyAdmin){
            return response()->json(['code' => 500, 'message' => '该人员不存在','data'=>null]);
        }
        $CompanyAdmin->role_id = $request->role_id;
        $CompanyAdmin->sex = $request->sex ?? 1;
        $CompanyAdmin->id_card = $request->id_card ?? '';
        $CompanyAdmin->idcard_file = $request->idcard_file ?? '';
        $CompanyAdmin->avatar = $request->avatar ?? '';
        if($request->entry_date){
            $CompanyAdmin->entry_date = $request->entry_date;
        }
        $CompanyAdmin->email = $request->email ?? '';
        $CompanyAdmin->edu = $request->edu ?? '';
        $CompanyAdmin->status = $request->status;

        try {
            DB::transaction(function () use ($CompanyAdmin,$Admin) {
                $CompanyAdmin->save();
                $Admin->save();
            });
            return response()->json(['code' => 200, 'message' => '操作成功','data'=>$CompanyAdmin]);
        } catch (\Throwable $th) {
            return response()->json(['code' => 500, 'message' => '网络异常','data'=>null]);
        }
    }

    public function updateAuth(Request $request)
    {
        $Admin = Admin::find(Auth::user()->id);
        if(empty($Admin)){
            return response()->json(['code' => 403, 'message' => '人员不存在','data'=>null]);
        }
        if($request->mobile != $Admin->mobile){
            $ismobile = Admin::where('mobile',$request->mobile)->first();
            if($ismobile){
                return response()->json(['code' => 403, 'message' => '手机号码已存在','data'=>null]);
            }
        }
        if($request->password){
            $Admin->password = Hash::make($request->password);
        }

        $Admin->name = $request->name;
        $Admin->mobile = $request->mobile;
        $Admin->save();
        return response()->json(['code' => 200, 'message' => '操作成功','data'=>$Admin]);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function delete($id)
    {
        if(!$id){
            return response()->json(['code' => 404, 'message' => '请选择要操作的记录']);
        }
        $data = CompanyAdmin::find($id);
        //企业总管理员无法删除
        if($data->role_id == 0){
            return response()->json(['code' => 404, 'message' => '企业总管理无法删除']);
        }
        $data->delete();
        return response()->json(['code' => 200, 'message' => '操作成功']);
    }

    public function show($id){
        $data = CompanyAdmin::with('admin')->find($id);
        $data->name = $data->admin->name;
        $data->mobile = $data->admin->mobile;
        $data->status = $data->status == 1 ? true : false;
        return response()->json(['code' => 200, 'message' => 'success','data'=>$data]);
    }

    public function info(){
        $data = CompanyAdmin::with('admin')->where('admin_id',Auth::user()->id)->first();
        $data->name = $data->admin->name;
        $data->mobile = $data->admin->mobile;
        $data->avatar = '';
        return response()->json(['code' => 200, 'message' => 'success','result'=>$data]);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function login(Request $Request){
        if(!$Request->username){
            return response()->json(['code' => '403', 'message' => '请输入用户名']);
        }
        if(!$Request->password){
            return response()->json(['code' => '403', 'message' => '请输入密码']);
        }

        $data = Admin::with('company')->where('mobile',$Request->username)->first();
        if(!$data){
            return response()->json(['code' => '404', 'message' => '账号不存在']);
        }
        $is = Hash::check($Request->password, $data->password);
        if(!$is){
            return response()->json(['code' => '500', 'message' => '密码错误']);
        }
        //判断权限
        $data->token = $data->createToken('admin_api')->accessToken;
        $data->type = 'admin-refresh';
        return response()->json(['code' => 200, 'message' => 'success','result'=>$data]);
    }


}
